<?php
include_once("Element.class.php");
include_once("Option.class.php");

class Elegible extends Element
{
	var $options = array();
	var $sql;
	var $conn;

	function Elegible($id, $options = array())
	{
		parent::Element($id);
		$this->options = $options;
	}

	function fromSQL($table = '', $id = '', $value = '', $where = '')
	{
		if (!empty($table))
		{
			$this->sql = "SELECT $id AS ID, $value AS FIELD FROM $table";
		}

		if ($where != '')
		{
			$this->sql .= " WHERE $where";
		}
		if (empty($this->sql))
		{
			die("SQL vacio!");
		}
		$query = mysql_query($this->sql, $this->conn);
		$options = array();

		while($row = mysql_fetch_object($query))
		{
			if (!empty($row->ID))
			{
				$options[$row->ID] = $row->FIELD;
			} else {
				$options[] = $row->FIELD;
			}
		}

		$this->value($options);
	}

	function value($options)
	{
		$this->options = array();
		foreach($options as $ID => $option)
		{
			$flag = (is_array($this->default) && in_array($ID, $this->default)) || $ID == $this->default;
			$this->options[] = new Option($ID, $option, $flag);
		}
	}
}

?>